<?php
/**
 * 
 * User/IndexAction.class.php (设计师模块)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2012-10-08 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class DesignerAction extends BaseAction
{

	function _initialize()
    {	
		parent::_initialize();
        $_GET =get_safe_replace($_GET);
        $this->url='index.php?g=Mobile&m=Designer&a=index';
		if(!$this->_userid){
			$this->assign('jumpUrl',U('Mobile/Login/index'));
			$this->error(L('nologin'));
			exit;
		}
        $id=intval($_GET['id']);
        if($id){
        $this->dao = M('Case');
        $list=$this->dao->field('userid')->where("id=".$id)->find();
        if($this->_userid!=$list['userid']){
            $this->error(L('出错啦'));
            exit;
        }}

        $this->dao = M('User');
        $this->assign('bcid',0);
        $user = $this->dao->find($this->_userid);
        $this->user=$user;
        if($user['groupid']!=1){
            $this->error(L('您不是设计师'));
            exit;
        }
        $this->assign('vo',$user);

		unset($_POST['status']);
		unset($_POST['groupid']);
		unset($_POST['amount']);
		unset($_POST['point']);

        
    }
//设计师个人作品
    public function index()
    {

            $this->dao = M('Case');
            $this->Urlrule =F('Urlrule');

            $where='userid='.$this->_userid;
            $count = $this->dao->where($where)->count();

            import ( "@.ORG.Page" );
            $listRows =  8;
            $page = new Page ( $count, $listRows );
            $pages = $page->preshow();
            //$field =  $this->module[$this->mod[$module]]['listfields'];
            $field =  "*";
            $order='listorder ,createtime desc,id desc';
            $list = $this->dao->field($field)->where($where)->order($order)->limit($page->firstRow . ',' . $page->listRows)->select();
            //echo $this->dao->getLastSql();exit;
            //print_r($pages);exit;
            $this->assign('title','设计作品');
            $this->assign('pages',$pages);
            $this->assign('list',$list);




        $this->display();
    }




    public function point()
    {
        $this->assign('menuWZ',20);
        $this->display();
    }

    public function edit_img(){
        $id=$_GET['id'];
        if($id){
            $this->dao = M('Case');
            $data=$this->dao->where("id=".$id)->find();
           // $pics=explode(":::",$list['pics']);

           // $p_data_arr=explode('|', $pics);
            $fields = F($this->mod['Case'].'_Field');
            foreach($data as $key=>$c_d){
                $setup='';
                $fields[$key]['setup'] =$setup=string2array($fields[$key]['setup']);
                if($setup['fieldtype']=='varchar' && $fields[$key]['type']!='text'){
                    $data[$key.'_old_val'] =$data[$key];
                    $data[$key]=fieldoption($fields[$key],$data[$key]);
                }elseif($fields[$key]['type']=='images' || $fields[$key]['type']=='files'){
                    if(!empty($data[$key])){
                        $p_data=explode(':::',$data[$key]);
                        $data[$key]=array();
                        foreach($p_data as $k=>$res){
                            $p_data_arr=explode('|',$res);
                            $data[$key][$k]['filepath'] = $p_data_arr[0];
                            $data[$key][$k]['filename'] = $p_data_arr[1];
                        }
                        unset($p_data);
                        unset($p_data_arr);
                    }
                }
                unset($setup);
            }
            //
            //print_r($data);exit;

            $this->assign('id',$id);
            $this->assign('pics',$data['pics']);
        }
        $this->display();


    }

    public function del_img(){


        $data=$_POST;

        foreach ($_POST['pics'] AS $k => $v) {

               // echo $v;exit;
                  if ($k == 0) {
                      $data['pics'] = '';
                  } else {
                      $r = ':::';
                  }
                  $data['pics'] .= $r . $v;

        }
       //print_r($data);exit;
        $this->dao = M('Case');
        $this->dao->update_time = time();
        $this->dao->last_ip = get_client_ip();
        $result = $this->dao->save($data);
        //echo $this->dao->getLastSql();exit;
        if (false !== $result) {
            $this->success(L('do_success'));
        } else {
            $this->error(L('do_error'));
        }


    }

    //添加修改页面
    public function edit(){
        $id=$_GET['id'];
        if($id){
        $this->dao = M('Case');
        $list=$this->dao->where("id=".$id)->find();
        $pics=explode(":::",$list['pics']);
        //print_r($pics);exit;
        $this->assign('list',$list);
        $this->assign('pics',$pics);
        }
        $this->display();
    }


//设计师上传作品
    public function save()
    {
        $this->dao = M('Case');
//exit;
        $this->assign('bcid', 0);
        $user = M('User')->find($this->_userid);
        $data=$_POST;
        

       // print_r($data);exit;
        if ($data) {

            if (!$data['title']) {
                    $this->error(L('标题不能为空'));
                    exit;
            }
            if (!$data['itemType']) {
                $this->error(L('请选择类型'));
                exit;
            }
            if (!$data['budget']) {
                $this->error(L('造价不能为空'));
                exit;
            }
            if (!$data['style']) {
                $this->error(L('请选择风格'));
                exit;
            }
            if (!$data['itemArea']) {
                $this->error(L('面积不能为空'));
                exit;
            }

            if (!$this->dao->create($data)) {
                $this->error($this->dao->getError());
            }
            //$this->dao->update_time = time();
           // $this->dao->last_ip = get_client_ip();


            foreach ($data['pics'] AS $k => $v) {
                 //echo $v;exit;
                if ($k == 0) {
                    $data['pics'] = '';
                }else{$r=':::';}
                 $img_url=tran($v);
               // echo $img_url;exit;
                $data['pics'] .= $r.$img_url ;
            }
            $data['thumb_list']=tran($data['thumb_list']);
            $data['pic']=tran($data['pic']);
            //print_r($data);exit;
            if ($data['id']) {
                $data['updatetime']=time();
                $result = $this->dao->save($data);
            } else {

                $data['userid']=$this->_userid;
                $data['username']=$user['username'];
                $data['createtime']=time();
                $data['status']='0';
                $data['catid']='1';
                $data['keywords']=$data['title'];
                $data['description']=$data['describe'];
                $result = $this->dao->add($data);
                M('User')->where("id=".$this->_userid)->setInc('case_num');

                //添加用户动态
                $userdate['title']='发布了【案例图】'.$data['title'];
                $userdate['url']='index.php?g=Home&m=Case&a=show&id='.$result;
                $userdate['userid']=$this->_userid;
                $userdate['conid']=$result;
				$userdate['module']='Case';
                $userdate['createtime']=time();
                $userdate['lang']='1';
                $userdate['status']='1';
                $ud=M("Dynamic")->add($userdate);



              }
            if (false !== $result) {
                $this->success(L('do_success'),$this->url);
            } else {
                $this->error(L('do_error'));
            }
            exit;
            }
       $this->display();
    }

    public function delete(){
        $id=$_GET['id'];
        $userid=$this->_userid;
        $dao= M('Case');
        $list=$dao->field('pics,thumb_list,pic')->where("id=".$id)->find();
        $pics=explode(":",$list['pics']);

        $result=$dao->where("id=".$id)->delete();
        M('User')->where('id='. $userid)->setDec('case_num');
        if($result){
            $pics=explode(":::",$list['pics']);
            @unlink('./'.$list['thumb_list']);
            @unlink($list['thumb_list']);
            @unlink('./'.$list['pic']);
            @unlink($list['pic']);
            foreach($pics as $k=>$v){
                $p_data_arr=explode('|',$v);
                $r=@unlink($p_data_arr[0]);
                $r=@unlink('./'.$p_data_arr[0]);

            }

        $this->success('操作成功', $this->url);
        }

    }







}

